import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class Checker {
    public static void main(String[] args) throws IOException {
        String directoryPath = "C:\\Users\\weixingfuj\\Documents\\0-2022-2023-2\\0-毕业设计指导\\电子资料归档\\"; // 替换成你要列出文件的目录路径
        List<String> list = readFileToList(System.getProperty("user.dir")+"/src/main/resources/student.txt");
        for (String str : list){
            showStudentFile(directoryPath + str +"\\");
        }

    }

    private static void showStudentFile(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            System.out.println("Directory not found: " + directoryPath);
            return;
        }
        if (directory.isDirectory()) {
            System.out.println("目录 " + directory);
            String s[] = directory.list();
            for (int i = 0; i < s.length; i++) {
                File f = new File(directory + "/" + s[i]);
                if (f.isDirectory()) {
                    System.out.println(s[i]);
                    File f1 = new File(directoryPath + "/" + s[i]);
                    try {
                        filterlist(directoryPath + "/" + s[i], f1);
                    }catch (Exception e){
                        System.out.println(e);
                    }
                } else {
                    System.out.println(s[i] + " 是一个文件");
                }
            }
        } else {
            System.out.println(directory + " 不是一个目录");
        }
        //filterlist(directoryPath, directory);
    }

    private static void filterlist(String directoryPath, File directory) throws IOException {
        File[] files = directory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                //System.out.println(name);
                return Pattern.matches("^.*[\\.docx|\\.pdf|\\.ppt|\\.pptx]?$", name); // 使用正则表达式匹配文件名
            }
        });
        if (files == null) {
            System.out.println("Failed to list documents in directory: " + directoryPath);
            return;
        }
        for (File file : files) {
            System.out.println("-- " + file.getName());
            //getSign(file.getName());
        }
    }

    private static List<String> readFileToList(String filename) {
        //filename = "student.txt";
        List<String> lines = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println("File contents:");
//        for (String s : lines) {
//            System.out.println(s);
//        }
        return lines;
    }

    private static void getSign(String filename) throws IOException {
        FileInputStream fis = new FileInputStream(filename);
        XWPFDocument doc = null;

        doc = new XWPFDocument(fis);

//        for (XWPFParagraph para : doc.getParagraphs()) {
//            if (para.getRuns().stream().anyMatch(run -> run.getEmbeddedPictures().size() > 0)) {
//                // 该段落包含图片
//                // 可以进一步处理图片或者抛出异常等操作
//                System.out.println("包含签名");
//                break;
//            }else{
//                System.out.println(para.getRuns());
//            }
//        }
        for (XWPFTable table : doc.getTables()) {
            for (XWPFTableRow row : table.getRows()) {

                    String text = row.toString();
                    System.out.println(text);

            }
        }
        fis.close();
    }
}

//getSign("C:\\Users\\weixingfuj\\Documents\\0-2022-2023-2\\0-毕业设计指导\\电子资料归档\\20170407430520+李泽林+19软件班\\1.开题资料\\开题报告.docx");